<?php

namespace App\Http\Controllers\Admin\Config\Attachment;

use Illuminate\Http\Request;
use App\Http\Controllers\AdminController;

use Symfony\Component\HttpFoundation\File\UploadedFile;

use App\Models\Attachment;

class UploadController extends AdminController
{


    /**
     * 图片验证
     * @param  Request $request [description]
     * @return [type]           [description]
     */
    private function fileValidator(array $data,$attachment){
        $attribute =  array(
            $attachment->uploadFileName => '文件',
        );

        $message  = array(
            'required' => ':attribute必须上传',
            'file' => '请上传文件',
            'mimes' => '请上传 xls、xlsx、doc、ppt类型文件'
        );

        return Validator::make($data, [
            $attachment->uploadFileName => 'bail|required|file|mimes:xls,xlsx,doc,ppt',
        ],$message,$attribute);
    }

    /**
     * 上传图片
     * @param  Request $request [description]
     * @return [type]           [description]
     */
    function uploadPic(Request $request){
    	$attachment = new Attachment();
        $attachment = $attachment->uploadPic($request);
        $this->ajaxJson(0,'上传图片成功',$attachment);
    }

    /**
     * 上传文件
     * @param  Request $request [description]
     * @return [type]           [description]
     */
    function uploadFile(Request $request){
        $data = $request->all();
        $attachment = new Attachment();
        $validator = $this->fileValidator($data,$attachment);
        if($validator->fails()){
            $this->ajaxJson(1,$validator->errors()->first());
        }
        if($request->hasFile($attachment->uploadFileName) && $request->file($attachment->uploadFileName)->isValid()){
            try {
                $file = $request->file($attachment->uploadFileName);
                $fileSize = $file->getClientSize();
                if($fileSize/1024>$attachment->fileMaxSize){
                    $this->ajaxJson(1,'文件上传过大,只允许'.($attachment->fileMaxSize/1024).'mb内的文件');
                }
                $fileArea = isset($data['fileArea']) ?  $data['fileArea'].'/' : '';

                $filePath  = date('Y',time()).'/'.date('m',time()).'/'.date('d',time())."/".date('H',time()).'/';
                $fileName              = date('YmdHis').str_random(15).'.'.$file->getClientOriginalExtension();

                $attachment->url       = $attachment->rootName.$fileArea.$filePath;
                $attachment->remoteUrl = '';
                $attachment->fileType  = $file->getClientMimeType();
                $attachment->fileName  = $fileName;
                $attachment->fileSize  = $fileSize;
                $attachment->isRemote = 0;
                $url = $fileArea.'/'.$filePath.$fileName;

                Storage::put($url,file_get_contents($file));
                $attachment->save();

            } catch (\Exception $e) {
                Log::error('【'.$e->getMessage().'】'.'【'.$e->getFile().'】'.'【'.$e->getTraceAsString ().'】');
                $this->ajaxJson(1,'上传文件异常,请重试');
            }
            $this->ajaxJson(0,'上传文件成功',$attachment);
        }
    }


    
}
